const err = require('./error')

module.exports = {

	// Toast
	toast_loading: function(content) {
		uni.showToast({
			icon: 'loading',
			title: content,
			fail: e=>{
				console.log(e)
			}
		})
	},
	toast_done: function(content) {
		uni.showToast({
			title: content,
		})
	},
	toast: function(content) {
		uni.showToast({
			icon: 'none',
			title: content,
		})
	},
	toast_hide: function() {
		uni.hideToast()
	},

	//Modal
	modal: function(title, content) {
		return new Promise((resolve, reject) => {
			uni.showModal({
				title: title,
				content: content,
				success: function(res) {
					resolve(res.confirm)
				},
				fail: function(e) {
					reject(err.Unknown_Sys_Failure(e))
				}
			})
		})
	},

	// ActionSheet
	chooseFromMenu: function(choiceList) {
		return new Promise((resolve, reject) => {
			uni.showActionSheet({
				itemList: choiceList,
				success: function(res) {
					resolve(choiceList[res.tapIndex])
				},
				fail: function(e) {
					resolve(false) // cancel goes here.
				}
			})
		})
	},

	// cache
	get_cache: function(key) {
		return uni.getStorageSync(key)
	},
	get_cache_async: function(key) {
		return new Promise((resolve, reject) => {
			uni.getStorage({
				key: key,
				success: function(res) {
					resolve(res.data)
				},
				fail: function(e) {
					if (e.errMsg == "getStorage:fail data not found") resolve(undefined)
					else reject(err.Unknown_Sys_Failure(e))
				}
			})
		})
	},
	set_cache: function(key, value) {
		uni.setStorageSync(key, value)
	},
	set_cache_async: function(key, value) {
		return new Promise((resolve, reject) => {
			uni.setStorage({
				key: key,
				success: function(res) {
					resolve()
				},
				fail: function(e) {
					reject(err.Set_Local_Cache_Failed(e))
				}
			})
		})
	},
	rm_cache: function(key) {
		uni.removeStorageSync(key)
	},
	rm_cache_async: function(key) {
		return new Promise((resolve, reject) => {
			uni.removeStorage({
				key: key,
				success: function(res) {
					resolve(res)
				},
				fail: function(e) {
					reject(err.Unknown_Sys_Failure(e))
				}
			})
		})
	},

	//navigation
	navigateTo: function(url) {
		uni.navigateTo({
			url: url,
		})
	},
	navigateBack: function() {
		uni.navigateBack({
			delta: 1
		})
	},
	redirectTo: function(url) {
		uni.redirectTo({
			url: url
		})
	},


	// shake
	addShakePhoneCallback: function(callbackFunc) {
		uni.onAccelerometerChange(function(e) {
			if (e.x > 0.6 && e.y > 0.6) {
				callbackFunc()
			}
		})
	},

	// location
	geocoding: function(address) {
		const sys = this
		return new Promise((s, f) => {
			sys.postUrl('https://www.water-crystal.org/WechatApp/Pray/geocoding', {address: address}).then(s).catch(e=>f(err.Baidu_Geocoding_Service_Error(e)))
		})
	},
	regeocoding: function(longitude, latitude) {
		const sys = this
		return new Promise((s, f) => {
			sys.postUrl('https://www.water-crystal.org/WechatApp/Pray/regeocoding', {lat: latitude, lon: longitude}).then(s).catch(e=>f(err.Baidu_Regeocoding_Service_Error(e)))
		})
	},
	openLocation: function(longitude, latitude) {
		return new Promise((resolve, reject) => {
			uni.openLocation({
				latitude: latitude,
				longitude: longitude,
				success: resolve,
				fail: function(e) {
					reject(err.Unknown_Sys_Failure(e))
				}
			})
		})
	},
	
	getUrl: function(url, data={}){
		return new Promise((resolve, reject) => {
			uni.request({
				url: url,
				data: data,
				success: (res) => {
					// console.log(res)
					resolve(res)
				},fail: (e) => {
					reject(err.Net_Access_Failure(e))
				}
			})
		})
	},
	
	postUrl: function(url, data, sendJson){
		return new Promise((resolve, reject) => {
			uni.request({
				method: 'POST',
				url: url,
				data: data,
				header:{
					'content-type': sendJson? 'application/json' : 'application/x-www-form-urlencoded'
				},
				success: (res) => {
					// console.log(res)
					resolve(res)
				},fail: (e) => {
					reject(err.Net_Access_Failure(e))
				}
			})
		})
	}
}
